<?php

class Gestff_GestauthController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function loginAction()
    {
    	$this->_helper->layout()->disableLayout();
		
		$this->_helper->layout()->setLayout("auth");
		
		$auth=new Application_Form_Authentification();
		$this->view->authform=$auth;
		 
    	if ($this->_request->isPost()) {
    		 
    		$formData = $this->_request->getPost();
    		 
    		if ($auth->isValid($formData)) {
    			
    			$DbAdapter=Zend_Db_Table::getDefaultAdapter();
    				
    			$authAdapter = new Zend_Auth_Adapter_DbTable($DbAdapter);
    			$authAdapter->setTableName('candidats');
    			$authAdapter->setIdentityColumn('LOGIN');
    			$authAdapter->setCredentialColumn('MOTDEPASSCRYPT');
    			
    			// Set the input credential values to authenticate against
    			$authAdapter->setIdentity($formData["email"]);
    			$authAdapter->setCredential($formData["password"]);
    			
    			// do the authentication
    			$auth = Zend_Auth::getInstance();
    			//die();
    			$result = $auth->authenticate($authAdapter);
    			
    			if ($result->isValid()) {
    				// success: store database row to auth's storage
    				// system. (Not the password though!)
    				$data = $authAdapter->getResultRowObject(null, 'password');
    				$auth->getStorage()->write($data);
    			//	Zend_Session::setOptions(array('userid'=>$data->idpersonnes));
    				$_SESSION['userid'] = $data->idcandidat;
    				
    				$this->_helper->redirector("cv","sinscrire","inscription");
    			} else {
    				// failure: clear database row from session
    				$this->view->message = 'Login failed.';
    			}
    			
    			
    		}
    	}
    	$this->view->title="Authentification";
    	$this->view->authform=$auth; 
    }

    public function logoutAction()
    {
        $this->_helper->layout()->disableLayout();
     	$this->_helper->viewRenderer->setNoRender(true);
    	
    	$auth = Zend_Auth::getInstance();
    	$auth->clearIdentity(); 
    	$this->_helper->redirector("login","authentification","pilotage");
    }

    public function creationcompteAction(){
    	 
    	$this->_helper->layout()->disableLayout();
    	$this->_helper->layout()->setLayout("inscription");
    	 
    	$form=new Application_Form_InscriptionFicheEtudiant();
    
    	$this->view->form=$form;
    	$db = Zend_Db_Table::getDefaultAdapter();
    	if($this->_request->isPost()){
    
    		if ($form->isValid( $this->getRequest()->getPost())) {
    			$dataposted=$this->getRequest()->getPost();
    
    			$candidatsModel		= new Application_Model_DbTable_Candidats();
    			$candidatsInfoFr	= new Application_Model_DbTable_Candidatinfos();
    			$candidatsInfoAr	= new Application_Model_DbTable_Candidatinfos();
    			$candidatstel		= new Application_Model_DbTable_Candidattels();
    
    				
    			$db->beginTransaction();
    				
    			try{
    					
    				$data=array(
    						'CPAYS'  => $form->getValue('Nationalite'),
    						'CIN' => $form->getValue('CIN') ,
    						'PASPORT' => "",
    						'NATIONALITE'=>$form->getValue('Nationalite'),
    						'DATENAISSANCE' =>$form->getValue('Datedenaissance') ,
    						'LOGIN' => $form->getValue('email'),
    						'MOTDEPASSCLAIR' =>  $form->getValue('password'),
    						'MOTDEPASSCRYPT' =>   $form->getValue('password'),
    						'PHOTO' =>  '',
    						'DATEINSCRIT'  => Zend_Date::now(),
    						'ADRESSMAPPOS'  =>  '',
    						'SEX' =>  $form->getValue('sex')
    
    				);
    					
    				$id=$candidatsModel->insert($data);
    					
    			}catch (Zend_Db_Exception $e) {
    				$this->view->messages = array('DbError' => $e->getMessage());
    				$db->rollBack();
    			}
    				
    				
    
    			try{
    
    				$data=array(
    						//'idcandidatinfos'
    						'idcandidat' =>$id
    						,'idlangues' =>'fr'
    						,'NOM'=>$form->getValue('nom')
    						,'PRENOM'=>$form->getValue('prenom')
    						,'ADRESS'=>""
    						,'NOMJEUNEFILLE'=>$form->getValue('nomjeunefille')
    						,'LIEUNAISSANCE'=>$form->getValue('Lieudenaissance')
    
    				);
    
    				$candidatsInfoFr->insert($data);
    
    			}catch (Zend_Db_Exception $e) {
    				$this->view->messages = array('DbError' => $e->getMessage());
    				$db->rollBack();
    			}
    
    			try{
    
    				$data=array(
    						//'idcandidatinfos'
    						'idcandidat' =>$id
    						,'idlangues' =>'ar'
    						,'NOM'=>$form->getValue('nom_ar')
    						,'PRENOM'=>$form->getValue('prenom_ar')
    						,'ADRESS'=>""
    						,'NOMJEUNEFILLE'=>$form->getValue('nomjeunefille_ar')
    						,'LIEUNAISSANCE'=>$form->getValue('Lieudenaissance_ar')
    
    				);
    
    				$candidatsInfoAr->insert($data);
    
    			}catch (Zend_Db_Exception $e) {
    				$this->view->messages = array('DbError' => $e->getMessage());
    				$db->rollBack();
    			}
    
    
    			/***********************************
    			 * Téléphone
    			*
    			***********************************/
    
    			try{
    
    				$data=array(
    						'idcandidat'=>$id
    						,'TEL' =>$form->getValue('Fax')
    						,'TYPE'=> 'FAX'
    				);
    					
    					
    				$candidatstel->insert($data);
    				 
    			}catch (Zend_Db_Exception $e) {
    				$this->view->messages = array('DbError' => $e->getMessage());
    				$db->rollBack();
    			}
    
    			try{
    					
    				$data=array(
    						'idcandidat'=>$id
    						,'TEL' =>$form->getValue('Telm')
    						,'TYPE'=> 'TELM'
    				);
    
    				$candidatstel->insert($data);
    
    			}catch (Zend_Db_Exception $e) {
    				$this->view->messages = array('DbError' => $e->getMessage());
    				$db->rollBack();
    			}
    			try{
    
    				$data=array(
    						'idcandidat'=>$id
    						,'TEL' =>$form->getValue('Telpro')
    						,'TYPE'=> 'TELM'
    				);
    
    				$candidatstel->insert($data);
    
    			}catch (Zend_Db_Exception $e) {
    				$this->view->messages = array('DbError' => $e->getMessage());
    				$db->rollBack();
    			}
    			$db->commit();
    			$this->_helper->redirector("parcoursetud");
    		}
     
    	}
   
    }
    
    
    
    
    public function inscriptionAction()
    {
        // action body
    	$this->_helper->layout()->disableLayout();
    	$this->_helper->layout()->setLayout("simplelayout");
    	
    	
    	
    	
    	
    	$addpersonne = new Application_Form_Addpersonnes();
    	
    	$this->view->form = $addpersonne;
    	
    	$header=array(
    			"idpersonnes" => "ID",
    			"nom" => "nom",
    			"prenom" => "prenom",
    			"login" => "login",
    			"motpass" => "motpass",
    			"mail" => "mail"
    	);
    	$usermapper= new Application_Model_Mapper_Personnes();
    	$select=$usermapper->fetchAllToArray();
    	$page=$this->_getParam('page',1);
    	$paginator = Zend_Paginator::factory($select);
    	$paginator->setItemCountPerPage(3);
    	$paginator->setCurrentPageNumber($page);
    	
    	$this->view->header=$header;
    	
    	$this->view->keyOfData="idpersonnes";
    	
    	Zend_View_Helper_PaginationControl::setDefaultViewPartial('/prototype/controls.phtml');
    	$this->view->paginator=$paginator;
    	
    	
    	
    	$operation=$this->getRequest()->getParam("operation");
    	$ids=$this->getRequest()->getParam("id");
    	if ($operation=="del"){
    			$personne = new Application_Model_Personnes();
    
    			$personne->setIdpersonnes($ids);
    
    			$personnes = new Application_Model_Mapper_Personnes();
    
    			$personnes->delete($personne);
    	
    			$this->_helper->redirector('inscription','gestauth','superadmin',array("id"=>$this->getRequest()->getParam("id")));
    	}elseif($operation=="modif"){
    		if ($this->getRequest()->isPost()) {
    			$formData = $this->getRequest()->getPost();
    			if ($addpersonne->isValid($formData)) {
    					
    				$personnemap = new Application_Model_DbTable_Personnes();
    		
    				$personnemap->update(
    						array(
	    						"mail"=>$addpersonne->getValue('mail'),
	    						"motpass"=>$addpersonne->getValue('motpass'),
	    						"login"=>$addpersonne->getValue('login'),
	    						"prenom" => $addpersonne->getValue('prenom'),
	    						"nom" => $addpersonne->getValue('nom')
    						),
    						"idpersonnes = '".$this->getRequest()->getParam("id")."'");
    		
    				$this->_helper->redirector("inscription",'gestauth','superadmin');
    			} else {
    				$addpersonne->populate($formData);
    			}
    		} else {
    			$id = $this->_getParam('id');
    			if (!empty($id)) {
    				$reqcomp = new Application_Model_DbTable_Personnes();
    				$data=$reqcomp->fetchRow("idpersonnes = '".$id."'")->toArray();
    					
    				$addpersonne->populate($data);
    			}
    		}
    	}
    	
    	
    	
    	
    	
    	if ($this->_request->isPost ())
    	{
    		$formData = $this->_request->getPost ();
    		 
    		if ($addpersonne->isValid ( $formData ))
    		{
    			$datas = $formData;
    	
    			$listpersonne= new Application_Model_DbTable_Personnes();
    	
    			$data=array(
    					"nom" => $datas['nom'],
    					"prenom" => $datas['prenom'],
    					"login" => $datas['login'],
    					"motpass" => $datas['motpass'],
    					"mail" => $datas['mail']
    			);
    	
    			$listpersonne->insert($data);
    			$this->_helper->redirector("inscription",'gestauth','superadmin');
    	
    		}
    	}
    }


}







